#ifndef __CMDCHANNEL_H__
#define __CMDCHANNEL_H__

#include "net/SocketTcpClient.h"
#include "com/MyThread.h"
#include "com/WaiterList.h"
#include "com/ListPtr.h"
#include "com/Lock.h"
#include "com/define.h"

class CMyManager;
class CCmdChannel : public CMyThread
	,IOTcpClientNotify
{
public:
	CCmdChannel(CMyManager *pMan);
	~CCmdChannel(void);

	bool Start(void);
	void Stop(void);

	bool GetValid(void);
	int SendUserVerify(const char *pUser, const char *pPassWord, char *outMsg, int *msgLen);

	bool GetUserInfo(char *outMsg, int *len);
	bool GetUserList(char *outMsg, int *len);

	bool GetMeetingFileList(char *outMsg, int *len);
	bool GetMeetingIssueList(char *outMsg, int *len);	

	bool GetMeetingVoteList(char *outMsg, int *len);
	bool TerminalUserSign(int nUserId, const char *pUserName, const char *pRemotePath);

	bool UserSignControl(int nControlType);
	bool AllUserSign(const char *pMsgJson);

	bool OpenScreenBroadcast();
	bool CloseScreenBroadcast(int nUserId);

	bool ReplyScreenBroadcastReq(int nUserId, int nResult);

	int InviteScreenBroadcast(int nUserId, int nType, bool bForce);
	int ScreenBroadcastControl(int nType);

	int UploadFileInfo(TerminalFileInfo *pTerminalFile);
	bool SendChatMessage(const char *pChatMessage);

	bool SendExtendData(const char *pChatMessage);

	int StartWbRequest(int nUserId, char *outMsg, int *len);
	int CloseWb(int nUserId);

	int IssueControl(int nIssueId, int nControlType);
	int VoteControl(int nVoteId, int nControlType);

	int VoteMsg(const char *pMsg, int nLen);
	int CenterControl(int nControlType);

	bool SendMeetingService(const char *pMessage);

protected:
	virtual void ThreadProcMain(void);
	virtual void OnRecvTcpServerDataNotify(const int nCmd, const int nPacketNum, const int nSendId, const char *szBuf, int nLen);
	virtual void OnDisConnTcpServerNotify(const int nType);

private:
	int ConnectServer(void);
	void HandleRecvData(RecvDataInfo *pRecvData);

	int SendData(int nCmd, const char *pData, int nDataLen);
	bool SendDataEx(int nCmd, const char *pData, int nDataLen, unsigned long &param);
	
	void HandleRspMsg(RecvDataInfo *pRecvData);
	void HandleChannelDeclareRsp(RecvDataInfo *pRecvData);

	bool SendChannelDeclare(void);
	bool SendHeartbetMsg(void);

	void ClearRecvData(RecvDataInfo *pRecvData);

private:
	CSocketTcpClient	*m_pClient;
	CMyManager			*m_pMan;

	CWaiterList			*m_pWaitList;

	bool				m_bExit;
	bool				m_bConnect;

	bool				m_bValid;
	bool				m_bFirstConnect;

	unsigned long		m_ulLastSendTime;
	unsigned long		m_ulLastConnectTime;

	unsigned short		m_usPacketNum;

	ListPtr				m_lstRecvData;
	CLock				m_lockRecvData;

};
#endif
